// @Description 测试控制器
// @Author  xiaoyang
// @Date  2025/11/26

package controllers

import (
	"github.com/gin-gonic/gin"
	"golang-manage-admin/http/modules/demo/services"
)

type TestController struct {
	Controller
	testService *services.TestService
}

// Init 初始化测试控制器
func (test *TestController) Init() {
	test.testService = &services.TestService{}
}

// TestMessage 测试HTTP访问
// @Tags     Test
// @Summary  测试HTTP访问接口
// @Description 用于测试HTTP连接和日志写入功能
// @Accept   application/json
// @Produce  application/json
// @Success  200  {object} controllers.ResponseFormat "测试成功"
// @Router   /test/test [post]
func (test *TestController) TestMessage(c *gin.Context) {
	// 调用 Service 层处理业务逻辑
	err := test.testService.TestMessage()
	if err != nil {
		test.BadResponse(50001, err.Error(), c)
		return
	}

	test.JsonResponse("", c)
}

// TestSendMessageDelayedByNet 测试批量发送延时消息
// @Tags     Test
// @Summary  测试批量发送延时消息接口
// @Description 批量投递10000个延时任务到队列中，用于测试延时消息功能
// @Accept   application/json
// @Produce  application/json
// @Success  200  {object} controllers.ResponseFormat "测试成功"
// @Router   /test/test-net [get]
func (test *TestController) TestSendMessageDelayedByNet(c *gin.Context) {
	// 调用 Service 层处理业务逻辑
	err := test.testService.BatchSendDelayedTasks()
	if err != nil {
		test.BadResponse(50002, err.Error(), c)
		return
	}

	test.JsonResponse("批量发送完成", c)
}

// TestSendMessageToQueue 测试消息队列推送
// @Tags     Test
// @Summary  测试消息队列推送接口
// @Description 推送测试消息到队列中，用于测试消息队列功能
// @Accept   application/json
// @Produce  application/json
// @Success  200  {object} controllers.ResponseFormat "推送成功"
// @Failure  500  {object} controllers.ResponseFormat "推送失败"
// @Router   /test/test-queue [get]
func (test *TestController) TestSendMessageToQueue(c *gin.Context) {
	// 调用 Service 层处理业务逻辑
	err := test.testService.SendMessageToQueue()
	if err != nil {
		test.BadResponse(50003, err.Error(), c)
		return
	}

	test.JsonResponse("", c)
}

// TestSendMessageExpiration 测试消息过期时间
// @Tags     Test
// @Summary  测试消息过期时间接口
// @Description 推送消息到死信队列中，测试Per-queue-Message-TTL功能
// @Accept   application/json
// @Produce  application/json
// @Success  200  {object} controllers.ResponseFormat "推送成功"
// @Failure  500  {object} controllers.ResponseFormat "推送失败"
// @Router   /test/test-queue-message-expiration [get]
func (test *TestController) TestSendMessageExpiration(c *gin.Context) {
	// 调用 Service 层处理业务逻辑
	err := test.testService.SendMessageExpiration()
	if err != nil {
		test.BadResponse(50004, err.Error(), c)
		return
	}

	test.JsonResponse("", c)
}

// TestSendMessageDelayed 测试延时消息推送
// @Tags     Test
// @Summary  测试延时消息推送接口
// @Description 推送延时消息到队列中，测试Per-message-TTL功能
// @Accept   application/json
// @Produce  application/json
// @Param    context query string true "消息内容"
// @Param    expires query string true "过期时间(毫秒)"
// @Success  200  {object} controllers.ResponseFormat "推送成功"
// @Failure  400  {object} controllers.ResponseFormat "参数错误"
// @Failure  500  {object} controllers.ResponseFormat "推送失败"
// @Router   /test/test-queue-message-delayed [get]
func (test *TestController) TestSendMessageDelayed(c *gin.Context) {
	context := c.Query("context")
	expires := c.Query("expires")

	if context == "" || expires == "" {
		test.BadResponse(10000, "参数错误", c)
		return
	}

	// 调用 Service 层处理业务逻辑
	err := test.testService.SendDelayedMessage(context, expires)
	if err != nil {
		test.BadResponse(50005, err.Error(), c)
		return
	}

	test.JsonResponse("", c)
}
